package com.satan.juc.demo24;

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

/**
 * @Author: Demon
 * @Date: 2024/3/30 01:24
 * @Description:
 **/
public class AtomicReferenceFieldUpdaterExample {
    public static void main(String[] args) {
        User user = new User("Alice", 25);
        AtomicReferenceFieldUpdater<User, Integer> atomicReferenceFieldUpdater = AtomicReferenceFieldUpdater.newUpdater(User.class, Integer.class, "age");
        boolean isUpdateSuccess = atomicReferenceFieldUpdater.compareAndSet(user, 25, 26);
        System.out.println("Is update successful? " + isUpdateSuccess);
        System.out.println("Updated user: " + user);
    }
    static class User {
        private String userName;
        private Integer age;

        public User(String userName, int age) {
            this.userName = userName;
            this.age = age;
        }

        @Override
        public String toString() {
            return "User{" +
                    "userName='" + userName + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
}
